﻿using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using RainEngine.SceneNodes;
using RainEngine.Controllers;
using RainEngine;
using RainEngine.SceneNodes.UI;

namespace Sample01
{
    public class RedPaddleController : Controller
    {
        #region 构造函数和成员变量

        /// <summary>
        /// 要控制的球拍
        /// </summary>
        private Renderable2DSceneNode  paddle;

        /// <summary>
        /// 球拍的移动速度
        /// </summary>
        const float paddleSpeed = 200.0f;
                
        /// <summary>
        /// 创建一个新Controller
        /// </summary>        
        public RedPaddleController(RainGE engine): base(engine)
        {
            
        }

        #endregion
        
        /// <summary>
        /// 控制逻辑
        /// </summary>
        /// <param name="gameTime"></param>
        public override void ControllerAction(GameTime gameTime)
        {
            paddle = (Renderable2DSceneNode )controlledNode;
            float moveFactorPerSecond = (float)gameTime.ElapsedGameTime.TotalMilliseconds / 1000.0f;
            ProcessInput(moveFactorPerSecond);
        }

        private void ProcessInput(float moveFactorPerSecond)
        {
            //按W键向上移动
            if (Input.Keyboard .IsKeyDown (Keys.W))
            {
                paddle.Position -= new Vector2(0, paddleSpeed*moveFactorPerSecond);
                if (paddle.Position.Y < 0)
                    paddle.Position= new Vector2 (10,0);                
                    
            }
            //按S键向下移动
            if (Input.Keyboard.IsKeyDown(Keys.S))
            {
                paddle.Position += new Vector2(0, paddleSpeed*moveFactorPerSecond);
                if(paddle .Position .Y>508)
                    paddle.Position =new Vector2 (10,508);                
            }
        }
    }
}
